home *** CD-ROM | disk | FTP | other *** search
/ The Business Master (3rd Edition) / The Business Master (3rd Edition).iso / files / business / stata2 / survive.kit < prev   
Encoding:
Text File  |  1988-08-16  |  8.4 KB  |  357 lines

  1. set output error
  2.  
  3. display _new in blue /*
  4. */ "Loading Survive.Kit Release 2.0  Demonstration Version" _n /*
  5. */ "Copyright (c) 1987-1988 by ==C=R=C==.  All rights reserved."
  6.  
  7.  
  8. /*
  9.   User's wishing to edit Survive.Kit please see comments at end of this file.
  10. */
  11.  
  12.  
  13. * Kaplan-Meier estimates
  14.  
  15.  
  16. capture program drop kapmeier
  17. program define kapmeier
  18.     di in blue "not available in demonstration version"
  19. end
  20.  
  21. * greenwood's formula
  22.  
  23. capture program drop gwood
  24. program define gwood
  25.     di in blue "not available in demonstration version"
  26. end
  27.  
  28.  
  29. cap program drop loglogs
  30. program define loglogs
  31.     if "%_*"=="" {
  32.         di in blu "loglogs survtime deadind [, title(string)]"
  33.         exit
  34.     }
  35.     mac def _options "Title(string) BY(string) *"
  36.     mac def _varlist "req ex"
  37.     mac def _if "opt"
  38.     mac def _in "opt"
  39.     parse "%_*"
  40.     parse "%_varlist", parse(" ")
  41.     capture {
  42.         if "%_title"=="" {
  43.             mac def _title "     Log-Log Survival Check for Weibull"
  44.         }
  45.         if "%_by"!="" {
  46.             mac def _byy "by(%_by)"
  47.         }
  48.         if "%_if`%_in"~="" {
  49.             keep %_if %_in
  50.             }
  51.         _survc %_1 %_2 %_by
  52.         gen _lls = log(-log(_surv))
  53.         gen _lt = log(%_1)
  54.         format _lls %9.0f
  55.         graph _lls _lt, title("%_title") %_options %_byy /*
  56.               */ l1("log(-log(survival))") b2("log(time)")
  57.  
  58.     }
  59.     cap drop _lt
  60.     cap drop _surv
  61.     cap drop _stds
  62.     cap drop _vlogs
  63.     cap drop _lls
  64. end
  65.  
  66. /*
  67.     Part 2 - Survival Curve Calculations
  68. */
  69.  
  70.  
  71. capture program drop _survc
  72. program define _survc
  73.     mac def _varlist "req ex"
  74.     mac def _if "opt"
  75.     mac def _in "opt"
  76.     parse "%_*"
  77.     cap drop _xi
  78.     cap drop _surv
  79.     cap drop _stds
  80.     cap drop _vlogs
  81.     capture {
  82.         if "%_3"!="" {
  83.             mac def _byc "by %_3:"
  84.         }
  85.         gen _stds = - %_2
  86.         sort %_3 %_1 _stds /* deaths first */
  87.         gen int _xi = 0
  88.         %_byc replace _xi = 1 %_if %_in
  89.         %_byc replace _xi = sum(_xi)
  90.         %_byc replace _xi = _xi[_N] + 1 - _xi
  91.         gen _surv = 1
  92.         replace _surv = (_xi - %_2) / _xi %_if %_in
  93.         %_byc replace _surv = _surv * _surv[_n-1] if _n>1
  94.         by %_3 %_1: replace _surv = _surv[_N]
  95.         label var _surv "Survival Probability"
  96.         replace _stds = %_2 / _xi / (_xi - 1)
  97.         replace _xi=-1 %if %in /* -1 ==> in the sample */
  98.         replace _stds=0 if _xi!=-1
  99.         %_byc gen _vlogs = sum(_stds)
  100.         by %_3 %_1: replace _vlogs = _vlogs[_N]
  101.         replace _surv=. if _xi!=-1
  102.         replace _vlogs=. if _xi!=-1
  103.         replace _stds = _surv*sqrt(_vlogs)
  104.         label var _stds "Greenwood Survival S.D."
  105.         label var _vlogs "Var(log(_surv))"
  106.         drop _xi
  107.     }
  108.     if _rc {
  109.         di in red "--Break--"
  110.     }
  111. end
  112.  
  113. capture program drop survcurv
  114. program define survcurv
  115.     if "%_*"=="" {
  116.         di in blu /*
  117.         */ "survcurv time_variable death_indicator[, by(varname)]"
  118.         exit
  119.     }
  120.     mac def _varlist "req ex"
  121.     mac def _options "BY(string)"
  122.     mac def _if "opt"
  123.     mac def _in "opt"
  124.     parse "%_*"
  125.     parse "%_varlist", parse(" ")
  126.     _survc %_1 %_2 %_by %_in %_if
  127.     if _rc==0 {
  128.         di in bl _new "Variables created:"
  129.         desc _stds _surv _vlogs
  130.     }
  131.     if _rc {
  132.         di in red "--Break--"
  133.         BREAK
  134.     }
  135. end
  136.  
  137. capture program drop survsum
  138. program define survsum
  139.     di in blue "not availble in demonstration version"
  140. end
  141.  
  142. /*
  143.     Cox Regression Adjunct
  144. */
  145.  
  146. capture program drop _coxvar
  147. program define _coxvar
  148.     mac def _varlist "req ex"
  149.     mac def _in "opt"
  150.     mac def _if "opt"
  151.     parse "%_*"
  152.     parse "%_varlist", parse(" ")
  153.     cap drop _rh
  154.     cap drop _temp
  155.     cap drop _rh1
  156.     cap drop baserelh
  157.     cap drop ownsurv
  158.     cap drop basesurv
  159.     cap drop basehaz
  160.     capture {
  161.         predict _rh            /* we'd like to know its cox */
  162.         cap gen _temp = -%_1
  163.         sort _temp %_2
  164.         gen _rh1 = sum(_rh)
  165.         by _temp: replace _rh1=_rh1[_N]
  166.         gen baserelh = 1
  167.         replace baserelh = exp(-%_2/_rh1) %_if %_in
  168.         label var baserelh "Cox Baseline Relative Hazard"
  169.         replace _temp = - %_2
  170.         sort %_1 _temp
  171.         replace _temp = 99 %_if %_in
  172.         gen basesurv = baserelh
  173.         replace basesurv = basesurv * basesurv[_n-1] if _n>1
  174.         label var basesurv "Cox Baseline Survival Prob"
  175.         by %_1: replace basesurv = basesurv[_N]
  176.         replace basesurv=. if _temp!=99
  177.         replace baserelh=. if _temp!=99
  178.         gen ownsurv = basesurv ^ _rh
  179.         label var ownsurv "Survival Probability"
  180.     }
  181.     if _rc==1 {
  182.         di in red "--- Break ---"
  183.         cap drop basesurv
  184.         cap drop basehaz
  185.         cap drop baserelh
  186.         cap drop _rh
  187.         cap drop _temp
  188.         cap drop _rh1
  189.         BREAK
  190.     }
  191.     cap drop _rh
  192.     cap drop _temp
  193.     cap drop _rh1
  194. end
  195.  
  196. capture program drop coxvar
  197. program define coxvar
  198.     if "%_*"=="" {
  199.         di in blu "coxvar survtime deadind"
  200.         exit
  201.     }
  202.     mac def _varlist "req ex"
  203.     mac def _in "opt"
  204.     mac def _if "opt"
  205.     parse "%_*"
  206.     parse "%_varlist", parse(" ")
  207.     capture _coxvar %_1 %_2 %_if %_in
  208.     if _rc==0 {
  209.         di in bl _new "Variables created:"
  210.         desc baserelh basesurv ownsurv
  211.     }
  212. end
  213.  
  214. cap program drop coxbase
  215. program define coxbase
  216.     if "%_*"=="" {
  217.         di in blu "coxbase survtime deadind [, title(string)]"
  218.         exit
  219.     }
  220.     mac def _options "Title(string) *"
  221.     mac def _varlist "req ex"
  222.     mac def _in "opt"
  223.     mac def _if "opt"
  224.     parse "%_*"
  225.     parse "%_varlist", parse(" ")
  226.     capture {
  227.         if "%_title"=="" {
  228.             mac def _title "     Baseline Survival Curve"
  229.         }
  230.         if "%_offset"=="" {
  231.             mac def _myaxis "noaxis xline(0) yline(0)"
  232.         }
  233.         _coxvar %_1 %_2 %_if %_in
  234.         graph basesurv %_1, c(l) title("%_title") /*
  235.          */  ysca(0,1) xsca(0,.) ytic(0,.25,.5,.75,1) %_options
  236.     }
  237.     cap drop basesurv
  238.     cap drop baserelh
  239.     cap drop ownsurv
  240. end
  241.  
  242. cap program drop coxhaz
  243. program define coxhaz
  244.     if "%_*"=="" {
  245.         di in blu "coxhaz survtime deadind [, title(string)]"
  246.         exit
  247.     }
  248.     mac def _options "Title(string) *"
  249.     mac def _varlist "req ex"
  250.     mac def _in "opt"
  251.     mac def _if "opt"
  252.     parse "%_*"
  253.     parse "%_varlist", parse(" ")
  254.     capture {
  255.         if "%_title"=="" {
  256.             mac def _title "     Baseline Hazard Function Estimate"
  257.         }
  258.         _coxvar %_1 %_2 %_if %_in
  259.         gen _b1 = %_1==%_1[_n-1]
  260.         replace _b1 = 1 if basesurv==basesurv[_n-1]
  261.         gen _t1 = 1
  262.         replace _t1 = 0 %_if %_in /* work across deleted obs */
  263.         replace _b1 = 1 if _t1==1
  264.         replace _t1 = %_1
  265.         replace _t1 = 0 if _n==1
  266.         replace _t1 = _t1[_n-1] if _b1
  267.         gen _t2 = basesurv
  268.         replace _t2 = 1 if _n==1
  269.         replace _t2 = _t2[_n-1] if _b1
  270.         gen basehaz = (_t2[_n-1]-_t2)/_t2[_n-1]/(_t1-_t1[_n-1]) if !_b1
  271.         label var basehaz "Cox Baseline Hazard Rate"
  272.         graph basehaz %_1, title("%_title") /*
  273.                */ ysca(0,.) xsca(0,.) xlabel ylabel %_options
  274.         drop _b1 _t1 _t2
  275.         di in blu _new "Variables created:"
  276.         noisily desc baserelh basesurv ownsurv basehaz
  277.         exit
  278.     }
  279.     di in red "--Break--"
  280.     cap drop _b1
  281.     cap drop _t1
  282.     cap drop _t2
  283.     cap drop basrelh
  284.     cap drop basesurv
  285.     cap drop ownsurv
  286.     cap drop basehaz
  287. end
  288.  
  289. /*
  290.    Part 3: Log Rank Test(s)
  291. */
  292.  
  293.  
  294. capture program drop logrank
  295. program define logrank
  296.     di in blue "not available in demonstration version"
  297. end
  298.  
  299.  
  300. capture program drop mantel
  301. program define mantel
  302.     di in blue "not available in demonstration version"
  303. end
  304.  
  305.  
  306. capture program drop wilcoxon
  307. program define wilcoxon
  308.     di in blue "not available in demonstration version"
  309. end
  310.  
  311.  
  312. di
  313. di in gr "The following new commands are now available:"
  314. di "   coxbase        gwood          loglogs        mantel        survsum"
  315. di "   coxhaz         kapmeier       logrank        survcurv      wilcoxon"
  316. di "   coxvar"
  317. di _n in gr "See " in wh "help survive.kit" in gr "."
  318. exit
  319.  
  320. Notes to User's wishing to edit this file:
  321.  
  322. These comments are placed at the end of the file, after the exit, so that
  323. Stata does not have to read through them every time the user types "run
  324. Survive.Kit"
  325.  
  326. Feel free to edit this file and delete programs in which you have no interest.
  327. Before editing this file, however, make sure the the original is BACKED UP.
  328. Please do not remove the copyright notice at the top of this file.
  329.  
  330. If you do edit the file, you should add a display command at the top of this
  331. file indicating that you have edited the file.  You should change the display
  332. just above these comments that lists the new commands now available.
  333.  
  334. All utility programs start with an underscore '_'.
  335.  
  336. Some programs use other programs in achieving the desired result.  Review the
  337. table below carefully before deleting programs.
  338.  
  339.         Program         Requires
  340.         --------------------------
  341.         coxbase         _coxvar
  342.         coxhaz          _coxvar
  343.         coxvar          _coxvar
  344.         gwood           _survc
  345.         kapmeier        _stemp1  _survc
  346.         loglogs         _survc
  347.         logrank
  348.         mantel
  349.         survcurv        _survc
  350.         survsum         _survc
  351.         wilcoxon
  352.  
  353. For instance, if you deleted the _coxvar program, you would break coxbase,
  354. coxhaz, and coxvar.
  355.  
  356. <end of Survive.Kit>
  357.